#ifndef __fpsmgr
#define __fpsmgr

#include <utils.h>

#include <console.h>
extern console cons;

class fpsmgr {

	clock_t start_time, end_time, elapsed_time;

	int fps;
	clock_t fps_time;

	bool paused;

	unsigned int frame;

	unsigned int evals;

	public:

		fpsmgr (int f = 100) {
			set_fps (f);
			elapsed_time = 0;
			paused = false;
			frame = 0;
			evals = 0;
		}

		void frame_starts () {
			start_time = clock();
		}

		void frame_ends () {
			if (!paused) {
				end_time = clock();
				clock_t frame_time = end_time - start_time;
				elapsed_time += frame_time;
				/*if (frame_time) {
          int got_fps = CLOCKS_PER_SEC / frame_time;
          if (got_fps < fps) cons << "asked fps = " << fps << ", got fps = " << got_fps << eol;
        }*/
				++frame;
			}
		}

		void pause () {
			paused = true;
		}

		void resume () {
			paused = false;
		}

		bool is_paused () const {
			return paused;
		}

		bool fps_reached () {
			if (elapsed_time >= fps_time) {
				++evals;
				elapsed_time -= fps_time;
				return true;
			} else return false;
		}

		int get_fps () const {
			return fps;
		}

		void set_fps (int f) {
			if (f < 1) f = 1;
			fps = f;
			fps_time = CLOCKS_PER_SEC / f;
			cons << cll << "fps = " << f;
		}

		void change_fps (int n) {
			set_fps (get_fps() + n);
		}

		unsigned int get_evals () {
			return evals;
		}

		void set_evals (unsigned int e) {
			evals = e;
		}

};


#endif
